'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 
        'datetimepicker' , 'datetimepickerCN'], function ($) {
  
  return {
    
    init: function () {
      this.initPage();
      this.initSearch();
      this.initRate();
      this.initAddSupplier();
      this.initExport();
    },

    initPage: function () {

      $(document).on("shown.bs.modal", "#edit", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
      });
      
      $(document).on("shown.bs.modal", "#appraisalResultDelete", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
        
      });
    },
    
    initRate: function () {

      $(document).on("shown.bs.modal", "#rate ", function (e) {

        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            $('.item-result').each(function(k, item) {
              var score = $(item).find('input[name="results"]').val();
              var id = $(item).find('input[name="resultIds"]').val();
              var itemResult = id + "|" + score;
              $(item).find('input[name="itemResults"]').val(itemResult);
            })
            
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id]
          }
        })
      });
      
    },

    initSearch: function () {
      $(document).on("shown.bs.modal", "#search", function (e) {
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
      });
    },
    
    initAddSupplier: function () {

      $(document).on("shown.bs.modal", "#result-create", function (e) {
        
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate

        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit: function (el) {
            
            var idArray = new Array();
            $('input[name="supplierIds"]:checked').each(function() {
              var id = $(this).val();
              idArray.push(id);
            });
            
            if (idArray.length == 0) {
              alert('至少选择一个供方!');
              return false;
            }
            
            return true;
            
          }
        })
        
      });
      
    },
    
    initSearch: function () {
      $(document).on("shown.bs.modal", "#export", function (e) {
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
      });
    }
    
  }
})